class MainSceneCotrol {
	private static _inst: MainSceneCotrol;
	private _mainScene: Carrun.UI_mainScene;
	//汽车数据数组字典数据
	public carDatasAry: Array<CarData>;
	//汽车item数组
	public carItemDatasAry: Array<CarItemModel>;
	private car1: Carrun.UI_carItem;
	//玩家拥有的金币数量
	public myMoney: number = 99999999;
	//玩家拥有的车辆数据
	public haveCarAry: Array<CarData>;
	public stage: egret.DisplayObjectContainer;
	private _dot: egret.Shape;
	/**当前拖动的item */
	private clickItem: CarItemModel;
	/**碰撞检测 碰撞到的carItem */
	private selectItem: CarItemModel;
	private carId: number = 8;
	/**汽车最高等级 */
	private maxLevel = 7;
	/**汽车最低等级 */
	private lowLevel = 0;
	public constructor() {
		fairygui.UIPackage.addPackage("carrun");
		Carrun.CarrunBinder.bindAll();
	}
	//单立
	public static inst(): MainSceneCotrol {
		if (!this._inst)
			this._inst = new MainSceneCotrol();
		return this._inst;
	}
	public get mainScene() {
		if (!this._mainScene)
			this._mainScene = Carrun.UI_mainScene.createInstance();
		return this._mainScene;
	}
	//初始化场景
	public initMainScene(value) {

		this._mainScene = Carrun.UI_mainScene.createInstance();
		this._mainScene.setSize(640, 1136);
		fairygui.GRoot.inst.addChild(this._mainScene);
		value.addChild(fairygui.GRoot.inst.displayObject);
		this.stage = value;
		//初始化页面数据
		this.carDatasAry = [new CarData(0, 5, 10, 10, "ui://mi4ig9lcko7m1", 1),
		new CarData(1, 4.5, 10, 20, "ui://mi4ig9lcko7m2", 2),
		new CarData(2, 4, 10, 30, "ui://mi4ig9lcko7m3", 3),
		new CarData(3, 3.5, 10, 40, "ui://mi4ig9lcko7m4", 4),
		new CarData(4, 3, 10, 50, "ui://mi4ig9lcko7m5", 5),
		new CarData(5, 2.5, 10, 60, "ui://mi4ig9lcko7m6", 6),
		new CarData(6, 2, 10, 70, "ui://mi4ig9lcko7m7", 7),
		new CarData(7, 1.5, 10, 80, "ui://mi4ig9lcko7m8", 8)];

		this.carItemDatasAry = [];

		var car1Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car1Model);
		car1Model.x = 217;
		car1Model.y = 489;

		var car2Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car2Model);
		car2Model.x = 284;
		car2Model.y = 489;

		var car3Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car3Model);
		car3Model.x = 353;
		car3Model.y = 489;

		var car4Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car4Model);
		car4Model.x = 217;
		car4Model.y = 557;

		var car5Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car5Model);
		car5Model.x = 284;
		car5Model.y = 557;

		var car6Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car6Model);
		car6Model.x = 353;
		car6Model.y = 557;

		var car7Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car7Model);
		car7Model.x = 217;
		car7Model.y = 623;

		var car8Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car8Model);
		car8Model.x = 284;
		car8Model.y = 623;

		var car9Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car9Model);
		car9Model.x = 353;
		car9Model.y = 623;

		var car10Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car10Model);
		car10Model.x = 217;
		car10Model.y = 689;

		var car11Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car11Model);
		car11Model.x = 284;
		car11Model.y = 689;

		var car12Model: CarItemModel = new CarItemModel();
		this._mainScene.m_carItemCom.addChild(car12Model);
		car12Model.x = 353;
		car12Model.y = 689;
		this.carItemDatasAry.push(car1Model);
		this.carItemDatasAry.push(car2Model);
		this.carItemDatasAry.push(car3Model);
		this.carItemDatasAry.push(car4Model);
		this.carItemDatasAry.push(car5Model);
		this.carItemDatasAry.push(car6Model);
		this.carItemDatasAry.push(car7Model);
		this.carItemDatasAry.push(car8Model);
		this.carItemDatasAry.push(car9Model);
		this.carItemDatasAry.push(car10Model);
		this.carItemDatasAry.push(car11Model);
		this.carItemDatasAry.push(car12Model);


		ShopWindowControl.inst().setCarList();
		this._mainScene.m_haveMoneyTxt.text = this.myMoney.toString();
		//初始化页面按钮监听事件
		this._mainScene.m_shopBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.showShopBtnClickHandler, this)
		this._mainScene.m_buyBtn.addEventListener(egret.TouchEvent.TOUCH_TAP, this.buyBtnClickHandler, this);
		this._dot = new egret.Shape;
		this._dot.graphics.beginFill(0x00ff00, 0);
		this._dot.graphics.drawRect(418, 504, 100, 190);
		this._dot.graphics.endFill();
		value.addChild(this._dot);
	}

	/**购买按钮点击事件*/
	private buyBtnClickHandler() {
		var t: fairygui.Transition = this._mainScene.getTransition("hintTween");

		if (this.myMoney < this.carDatasAry[0].buyMoney) {
			this._mainScene.m_hintTxt.text = "金币不足";
			t.play();
			return;
		}
		var haveItem: boolean = false;
		for (var i: number = 0; i < this.carItemDatasAry.length; i++) {
			if (this.carItemDatasAry[i].carData == null) {
				this.carId++;
				this.carItemDatasAry[i].carData = new CarData(0, 5, 10, 10, "ui://mi4ig9lcko7m1", this.carId);

				(this.carItemDatasAry[i].carItem as Carrun.UI_carItem).m_carImg.url = this.carDatasAry[0].carImgUrl;
				this.carItemDatasAry[i].addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.carItemTouchBeginHandler, this);
				haveItem = true;
				break;
			}
		}
		if (haveItem) {
			this.myMoney -= this.carDatasAry[0].buyMoney;
			this._mainScene.m_haveMoneyTxt.text = this.myMoney.toString();
		} else {
			this._mainScene.m_hintTxt.text = "没有空闲格子";
			t.play();
		}

	}
	/**商店按钮点击事件 */
	private showShopBtnClickHandler() {
		ShopWindowControl.inst().openShopWin();
	}

	/**汽车item鼠标按下事件 */
	private carItemTouchBeginHandler(value: egret.TouchEvent) {
		this.clickItem = value.target;
		this._mainScene.addEventListener(egret.TouchEvent.TOUCH_MOVE, this.carItemTouchMoveHandler, this);
		this._mainScene.addEventListener(egret.TouchEvent.TOUCH_END, this.carItemTouchEndHandler, this);
		this._mainScene.m_carImg.x = value.stageX;
		this._mainScene.m_carImg.y = value.stageY;
		this._mainScene.m_carImg.url = (this.clickItem.carItem as Carrun.UI_carItem).m_carImg.url;
	}


	/**汽车item鼠标移动事件 */
	private carItemTouchMoveHandler(value: egret.TouchEvent) {
		this._mainScene.m_carImg.x = value.stageX;
		this._mainScene.m_carImg.y = value.stageY;
		var bol: boolean = this._dot.hitTestPoint(this._mainScene.m_carImg.x, this._mainScene.m_carImg.y, false);
		var hit: boolean;
		var isHit = 0
		for (var i: number = 0; i < this.carItemDatasAry.length; i++) {
			hit = this.isHit(this.carItemDatasAry[i].x, this.carItemDatasAry[i].y, this._mainScene.m_carImg);
			if (hit) {
				this.selectItem = this.carItemDatasAry[i];
				(this.selectItem.carItem as Carrun.UI_carItem).m_selectImg.visible = true;
				isHit = 1;
			} else {
				(this.carItemDatasAry[i].carItem as Carrun.UI_carItem).m_selectImg.visible = false;
			}
		}
		if (isHit == 0) {
			this.selectItem = null;
		}
	}
	/**汽车item中鼠标抬起时间 */
	private carItemTouchEndHandler(value: egret.TouchEvent) {
		this._mainScene.removeEventListener(egret.TouchEvent.TOUCH_MOVE, this.carItemTouchMoveHandler, this);
		this._mainScene.removeEventListener(egret.TouchEvent.TOUCH_END, this.carItemTouchEndHandler, this);
		this._mainScene.m_carImg.url = "";
		this._mainScene.m_carImg.x = 0;
		this._mainScene.m_carImg.y = 0;

		if (!this.selectItem) {
			return;
		}


		(this.selectItem.carItem as Carrun.UI_carItem).m_selectImg.visible = false;
		if (this.selectItem.carData == null) {	//如果碰撞的item中没有汽车。则把当前拖动的车挪到此格
			this.selectItem.carData = this.clickItem.carData;
			(this.selectItem.carItem as Carrun.UI_carItem).m_carImg.url = (this.clickItem.carItem as Carrun.UI_carItem).m_carImg.url;
			this.selectItem.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.carItemTouchBeginHandler, this);
			this.clickItem.removeEventListener(egret.TouchEvent.TOUCH_BEGIN, this.carItemTouchBeginHandler, this);
			(this.clickItem.carItem as Carrun.UI_carItem).m_carImg.url = null;
			this.clickItem.carData = null;

		} else {
			//如果碰撞的item中有汽车。则判断当前拖动的车等级与碰撞item车的等级是否相同 相同则合并。升级 不相同则不做任何处理
			if (this.selectItem.carData.id == this.clickItem.carData.id)//拖拽的item与碰撞的id为同一个
			{

			} else if (this.selectItem.carData.level == this.clickItem.carData.level && this.selectItem.carData.level < this.maxLevel)//拖拽的item与碰撞的item等级相同则升级
			{
				this.selectItem.carData.level++;
				(this.selectItem.carItem as Carrun.UI_carItem).m_carImg.url = this.carDatasAry[this.selectItem.carData.level].carImgUrl;
				this.selectItem.carData.id = this.carId++;
				(this.clickItem.carItem as Carrun.UI_carItem).m_carImg.url = "";
				this.clickItem.carData = null;
			}
			this.selectItem = null;
			this.clickItem = null;
		}

	}
	/**碰撞检测 */
	private isHit(x, y, item, w = 59, h = 59) {
		var hit: boolean = false;
		if (item.x > x && item.y > y && item.x < (x + w) && item.y < (y + h)) {
			hit = true;
			return hit;
		}
		return hit;
	}
	private runningCar()
	{
		var t:fairygui.GTweener
	}
}